package br.com.cast.scc.pages.veiculos;

import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.form.AjaxFormComponentUpdatingBehavior;
import org.apache.wicket.ajax.markup.html.form.AjaxButton;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.form.ChoiceRenderer;
import org.apache.wicket.markup.html.form.DropDownChoice;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.IChoiceRenderer;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.spring.injection.annot.SpringBean;

import br.com.cast.scc.enuns.StatusRegistroAtivo;
import br.com.cast.scc.model.DominioTipoVeiculo;
import br.com.cast.scc.model.Veiculo;
import br.com.cast.scc.service.TipoVeiculoService;
import br.com.cast.scc.service.VeiculoService;

public class CadastrarVeiculoForm extends Form<Veiculo> {

	private static final long serialVersionUID = 1L;

	@SpringBean(name = "tipoVeiculoServiceImpl")
	private TipoVeiculoService tipoVeiculoService;

	@SpringBean(name = "veiculoServiceImpl")
	private VeiculoService veiculoService;

	private WebMarkupContainer panelCamposTerrestres;

	private WebMarkupContainer panelCamposFluviais;

	public CadastrarVeiculoForm(String id) {
		super(id);
		
//		add(new FeedbackPanel("msgErro", 
//				new ExactLevelFeedbackMessageFilter(FeedbackMessage.ERROR)));
//		
//		add(new FeedbackPanel("msgSucesso", 
//				new ExactLevelFeedbackMessageFilter(FeedbackMessage.SUCCESS)));


		criaLinkSalvar();

		criaLinkVoltar();

		panelCamposTerrestres = new WebMarkupContainer("panelCamposTerrestres");
		panelCamposTerrestres.setVisible(false);
		panelCamposTerrestres.setOutputMarkupPlaceholderTag(true);

		panelCamposFluviais = new WebMarkupContainer("panelCamposFluviais");
		panelCamposFluviais.setVisible(false);
		panelCamposFluviais.setOutputMarkupPlaceholderTag(true);

		setDefaultModel(new CompoundPropertyModel<Veiculo>(new Veiculo()));

		IChoiceRenderer<DominioTipoVeiculo> renderer = new ChoiceRenderer<>(
				"descricao");

		final DropDownChoice<DominioTipoVeiculo> tipoVeiculo = new DropDownChoice<DominioTipoVeiculo>(
				"tpVeiculo", tipoVeiculoService.consultaTodos(), renderer);

		tipoVeiculo.add(new AjaxFormComponentUpdatingBehavior("change") {

			private static final long serialVersionUID = 1L;

			@Override
			protected void onUpdate(AjaxRequestTarget target) {
				DominioTipoVeiculo tipo = tipoVeiculo.getModelObject();

				boolean isTerrestre = tipo.getCodigo().equals(
						DominioTipoVeiculo.COD_TERRESTRE);

				panelCamposTerrestres.setVisible(isTerrestre);
				panelCamposFluviais.setVisible(!isTerrestre);

				target.add(panelCamposTerrestres, panelCamposFluviais);
			}
		});

		add(tipoVeiculo);
		
		add(new TextField<>("endAcessoLink"));
		add(new TextField<>("loginAcesso"));
		add(new TextField<>("senhaAcesso"));


		panelCamposFluviais.add(new TextField<>("nuEmbarcacao"));

		panelCamposTerrestres.add(new TextField<>("placa1"));

		panelCamposTerrestres.add(new TextField<>("rntrc"));

		add(panelCamposFluviais, panelCamposTerrestres);

	}

	private void criaLinkSalvar() {

		add(new AjaxButton("salvar"){

			private static final long serialVersionUID = 1L;
			
			
			@Override
			protected void onSubmit(AjaxRequestTarget target, Form<?> form) {
				Veiculo veiculo = (Veiculo)form.getDefaultModelObject();
				veiculo.setStatus(StatusRegistroAtivo.ATIVO.getSituacao());
				veiculoService.salvar(veiculo);

			}
		});
	}

	private void criaLinkVoltar() {
		add(new Link("voltar") {

			private static final long serialVersionUID = 1L;

			@Override
			public void onClick() {
				setResponsePage(ConsultarVeiculoIndexPage.class);
			}

		});

	}
	


}
